/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: WHY
 * Date: 2023-03-13
 * Time: 21:58
 */
public class TestDemo {


    class MyThread extends Thread {
        @Override
        public void run() {
            while (true) {
                System.out.println("hello t");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public class ThreadDemo1 {
        public static void main(String[] args) {
            Thread t = new MyThread();
            // start 会创建新的线程
            t.start();
            // run 不会创建新的线程. run 是在 main 线程中执行的~~
            // t.run();

            while (true) {
                System.out.println("hello main");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    class MyRunnable implements Runnable {
        @Override
        public void run() {
            while (true) {
                System.out.println("hello t");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public class ThreadDemo2 {
        public static void main(String[] args) {
            MyRunnable runnable = new MyRunnable();
            Thread t = new Thread(runnable);
            t.start();

            while (true) {
                System.out.println("hello main");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public class ThreadDemo3 {
        public static void main(String[] args) {
            Thread t = new Thread() {
                @Override
                public void run() {
                    while (true) {
                        System.out.println("hello t");
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            };

            t.start();

            while (true) {
                System.out.println("hello main");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public class ThreadDemo4 {
        public static void main(String[] args) {
            Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        System.out.println("hello t");
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
            t.start();

            while (true) {
                System.out.println("hello main");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public class ThreadDemo5 {
        public static void main(String[] args) {
            Thread t = new Thread( () -> {
                while (true) {
                    System.out.println("hello t");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            } );

            t.start();

            while (true) {
                System.out.println("hello main");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public class ThreadDemo6 {
        public static void main(String[] args) {
            Thread t = new Thread(() -> {
                while (true) {
                    System.out.println("hello t");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }, "我的线程");

            t.start();
        }
    }

    public class ThreadDemo7 {
        public static void main(String[] args) {
            Thread t = new Thread(() -> {
                System.out.println("hello t");
            });

            t.start();

            try {
                // 上述 t 线程没有进行任何循环和 sleep, 意味着里面的代码会迅速执行完毕.
                // main 线程如果 sleep 结束, 此时 t 基本上就是已经执行完了的状态. 此时 t 对象还在
                // 但是在 系统中 对应的线程已经结束了.
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println(t.isAlive());
        }
    }


    public class ThreadDemo8 {
        public static void main(String[] args) {
            Thread t = new Thread(() -> {
                while (true) {

                }
            });

            // 默认是前台线程, 也就是设为 false
            // 此时这个线程会阻止进程结束

            // 改成 true 变成后台线程. 不影响进程的结束.
            t.setDaemon(true);
            t.start();

        }
    }
}
